<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","debtur_id");
$FileName      = "ais_setup_debtur.php";
$FileTemplate  = "ais_setup_debtur_form.html";
$SubHeaderTitle = "Debtur";
$TableName      = "tbldebtur";
$HTMLFormName   = "CustForm";

$FormAction    = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType   = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;
$FormFields    = array("CUCODE","CUNAME","CUADDR","CUZIP","CNCODE","CUPHONE","CUFAX","CUCONTACT","CUEMAIL","CUNPWP","CUSKADA","CUDEBTUR","CUDEBTUR_FIN","TOP","CUNPPKP","AFILIASI", "LBCODE");
$IS_ADMIN = (GetGroupID() == 10000) ? 'Ya' : 'Tidak';

if($IS_ADMIN=='Ya'){
    //$FormFields = array_merge($FormFields,array('BALANCE_IDR','BALANCE_USD','BALANCE_USD_RUPIAH','BALANCE_DATE'));
    $FormFields = array_merge($FormFields,array('BALANCE_IDR','BALANCE_USD','BALANCE_DATE'));
}
buildsqlfield ("$TableName", $FormFields);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name");
$FormCValue    = BuildHTMLField($FormFields,"value");
//$FDept           = GetParam("deptSearch","");

//Edited by Krisna Thursday 20 Oktober 2011
$SQLGet = "SELECT CUCODE FROM ".$TableName;
$arrProfileId = $DBConnection->dbc->get_row($SQLGetProfileId,ARRAY_A);

//fetch profile ID
$profileId = $arrProfileId['CUCODE'];
//Edited by Krisna Thursday 20 Oktober 2011
//
//************ FORM ACTION **************
if (GetParam("form","")==$HTMLFormName) {
    if (GetParam("Action","")=="Add") {
        FormInsert();
    }

    if (GetParam("Action","")=="Update") {
        $FormMode="Edit";
        //Edited By Krisna Thursday 20 October 2011
		//FormUpdate();
		
		$DBConnection->dbc->query("UPDATE ".$TableName." SET 
        CUCODE='".GetParam("CUCODE","")."',
        CUNAME='".GetParam("CUNAME","")."',
        CUADDR='".GetParam("CUADDR","")."',
        CUZIP='".GetParam("CUZIP","")."',
        CNCODE='".GetParam("CNCODE","")."',
        CUPHONE='".GetParam("CUPHONE","")."',
		CUFAX='".GetParam("CUFAX","")."',
        CUCONTACT='".GetParam("CUCONTACT","")."',
        CUEMAIL='".GetParam("CUEMAIL","")."',
		CUNPWP='".GetParam("CUNPWP","")."',
		CUSKADA='".GetParam("CUSKADA","")."',
		CUDEBTUR='".GetParam("CUDEBTUR","")."',
		CUDEBTUR_FIN='".GetParam("CUDEBTUR_FIN","")."',
		TOP='".GetParam("TOP","")."',
		CUNPPKP='".GetParam("CUNPPKP","")."',
		AFILIASI='".GetParam("AFILIASI","")."',
		LBCODE='".GetParam("LBCODE","")."'
        WHERE CUCODE='".$profileId."'");
		//Edited By Krisna Thursday 20 October 2011
    }

    if (GetParam("Action","")=="Delete") {
        $FormMode="Edit";
        FormDelete();
    }

    if (GetParam("Action","")=="Cancel") {
        $FormMode="Edit";
        FormCancel();
    }
}

//Field Verification
$Verification  = "Enable";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
//$VerifyFields  = array("CUDEBTUR"=>"Customer Code","CUNAME"=>"Customer Name","CUADDR"=>"Address","CNCODE"=>"Country","TOP"=>"Term Of Payment","AFILIASI"=>"Jenis Afiliasi Debtur");
$VerifyFields  = array("CUDEBTUR"=>"Customer Code","CUNAME"=>"Customer Name","CUADDR"=>"Address","CNCODE"=>"Country","TOP"=>"Term Of Payment");
VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar);

//************ SET FORM MODE ***************
// PENAMBAHAN SKRIP DISINI \\
if (GetParam(PRIMARY_KEY,"")) {
        $FormMode = "Edit";

} else {
        $FormMode = "Add";

}

Init($SessionVar,CreateVerifyLoader ($FileName,"iLoader",PRIMARY_KEY,"$HTMLFormName.".PRIMARY_KEY, $FormMode));
$InitFunction = "isExist".PRIMARY_KEY."()";

//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM $TableName";
if (GetParam(PRIMARY_KEY,"") && $FormMode="Edit") {
    $SQLForm = $SQL." WHERE ".PRIMARY_KEY."='".trim(GetParam(PRIMARY_KEY,""))."'";
}
$SQLCountry = "SELECT * FROM $DBGeneral.tblcountry where (CNDESC is not null or CNDESC='') order by CNDESC";
$SQLLineBusiness = "SELECT * FROM tbllinebusiness where (linebusiness_code is not null or linebusiness_code='') order by linebusiness_code";

//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

//HTML Pulldown
$TBS->MergeBlock("Countryblk",$DBMysql,$SQLCountry);
$TBS->MergeBlock("LineBusinessblk",$DBMysql,$SQLLineBusiness);

if (GetParam(PRIMARY_KEY,"")) $TBS->MergeBlock("blv","mysql",$SQLForm);
else $TBS->MergeBlock("blv",$FormCValue);
//HTML Name
$TBS->MergeBlock("bln",$FormCName);
$TBS->Show();


//*************** ACTION FUNCTION *******************
function FormInsert() {
    global $DBConnection,$FileName,$TableName;

    $getCuDebtur = $_POST['CUDEBTUR'];
    
    /*
    if($_POST[AFILIASI] == Ya){
        $getCuDebtur = "9".$getCuDebtur;
    }
    */

    //echo "cudebtur yang benar: $getCuDebtur";
    //exit;

    /* existing SQL (before editing for mpc)
    $SQL = "INSERT INTO $TableName(CUCODE,CUTYPE,CUNAME,CUADDR,CUZIP,CNCODE,CUPHONE,CUFAX,CUCONTACT,CUEMAIL,CUNPWP,CUSKADA,CUDEBTUR,TOP,CUNPPKP,AFILIASI)
            Values('$_POST[CUDEBTUR]','1000','$_POST[CUNAME]','$_POST[CUADDR]','$_POST[CUZIP]','$_POST[CNCODE]','$_POST[CUPHONE]','$_POST[CUFAX]','$_POST[CUCONTACT]','$_POST[CUEMAIL]','$_POST[CUNPWP]','$_POST[CUSKADA]','$_POST[CUDEBTUR]','$_POST[TOP]','$_POST[CUNPPKP]','$_POST[AFILIASI]')";
    */

    $SQL = "INSERT INTO $TableName(CUCODE,CUTYPE,CUNAME,CUADDR,CUZIP,CNCODE,CUPHONE,CUFAX,CUCONTACT,CUEMAIL,CUNPWP,CUSKADA,CUDEBTUR,CUDEBTUR_FIN,TOP,CUNPPKP,AFILIASI, LBCODE)
            Values('".$getCuDebtur."','1000','".$_POST['CUNAME']."','".$_POST['CUADDR']."','".$_POST['CUZIP']."','".$_POST['CNCODE']."','".$_POST['CUPHONE']."','".$_POST['CUFAX']."','".$_POST['CUCONTACT']."','".$_POST['CUEMAIL']."','".$_POST['CUNPWP']."','".$_POST['CUSKADA']."','".$getCuDebtur."','".$_POST['CUDEBTUR_FIN']."','".$_POST['TOP']."','".$_POST['CUNPPKP']."','".$_POST['AFILIASI']."', '".$_POST['LBCODE']."')";
    //die($SQL);
    $DBConnection->dbc->query($SQL);
    // ********** TAMBAHAN KE OTHER *******************
    //$SQL = "INSERT INTO tblother (kdother,nama,alamat1) SELECT cucode,cuname,cuaddr from tbldebtur";
    //$DBConnection->dbc->query($SQL);


    header("Location: $FileName?PageMode=Form");
    exit;
}

function FormUpdate() {
    global $DBConnection;
    $DBConnection->Update();

}

function FormDelete() {
    global $DBConnection,$FileName,$TableName;
    $DBConnection->Delete("$TableName",PRIMARY_KEY);
    header("Location: $FileName");
    exit;
}

function FormSearch () {

}

function FormCancel () {
    global $FileName;
    header("Location: ".$FileName);
    exit;
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
        global $DBConnection;
        for ($x=0;$x<count($arrayfield);$x++) {
            $DBConnection->add_htmlfield($table,$arrayfield[$x],GetParam($arrayfield[$x],""));
        }
}

?>
